/**
 * 显示消息提示框
 * @param content 提示的标题
 */
const toast = (content)=> {
	uni.showToast({
		icon: 'none',
		title: content
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
const showConfirm = (content) => {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
const tansParams = (params) => {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}


const calculateTimeDuration = (startTime, endTime) => {
    // 将时间字符串转换为 Date 对象
    const start = new Date(startTime);
    const end = new Date(endTime);
    
    // 计算时间差（毫秒）
    const timeDiff = end - start;
    
    // 如果结束时间早于开始时间，返回错误
    if (timeDiff < 0) {
        throw new Error("结束时间不能早于开始时间");
    }
    
    // 计算小时和分钟
    const totalMinutes = Math.floor(timeDiff / (1000 * 60));
    const hours = Math.floor(totalMinutes / 60);
    const minutes = totalMinutes % 60;
    
    // 返回格式化结果
    return {
        hours: hours,
        minutes: minutes,
        totalMinutes: totalMinutes,
        toString: function() {
            return `${hours}小时${minutes}分`;
        }
    };
}



const uploadFile = (url) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: 'https://admin.yaojunzhiku.com/prod-api/common/uploadOSS',
			filePath: url,
			name: 'file',
			header: {
				'content-type': 'multipart/form-data'
			},
			success: (resp) => {
				resolve(resp.data);
			},
		})
	})
}

// 使用示例
// const start = "2025/08/03 11:00";
// const end = "2025/08/03 18:15";

// 验证手机号方法

// const phone = "12345678901";
// const isPhone = /^1[3456789]\d{9}$/.test(phone); // true
// console.log(isPhone);


export default {
	toast,
	showConfirm,
	tansParams,
	calculateTimeDuration,
	uploadFile
}